﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Currency.TextUtils;
using CatUtils.Utils.Helper;
using CatUtils.Utils.SpecialUtils.Html;
using CatUtilsDll.Utils.Currency.WebUtils;

using System;
using System.Collections.Generic;
using System.Threading;

namespace SinaImgUpload.SainTool.LocalTool
{
    internal class ClearWithSize
    {
        /// <summary>
        /// 待清理文件夹
        /// </summary>
        /// <param name="FloderPath"></param>
        /// <param name="ProportionCount"></param>
        public void XmlFloder(string FloderPath, float ProportionCount)
        {
            List<string> FoloderList = FloderNameHelper.GetFullFileNameList(FloderPath, "*.lst");
            foreach (string line in FoloderList)
            {
                Console.WriteLine(line);
                XmlFile(line, ProportionCount);
            }
        }

        /// <summary>
        /// 单个XML文件
        /// </summary>
        /// <param name="XmlFilePath"></param>
        /// <param name="ProportionCount"></param>
        public void XmlFile(string XmlFilePath, float ProportionCount)
        {
            List<string> ImgList = HtmlHelper.GetHtmlValueList(FileHelper.ReadAllText(XmlFilePath), "//bed");
            int UploadCount = 0;
            List<string> CompltedList = new List<string>();
            foreach (string SinleImgInfo in ImgList)
            {
                new PrintLog().PercentLog(ImgList.Count, ++UploadCount);
                Console.WriteLine("待写入：" + CompltedList.Count);

                if (SinleImgInfo == "" || FileNameHelper.GetFileType(SinleImgInfo) == ".zip")
                    continue;
                string SinaImg = TextHelper.ReplaceTextEnhance(HtmlHelper.GetFirstText(SinleImgInfo, "//bed/sina"), "\\(.*?\\)", "")
                    .Replace("/large/", "/orj360/").Replace("https", "http");

                string OrgImg = HtmlHelper.GetFirstText(SinleImgInfo, "//bed/original");
                if (WebModeExt(OrgImg))
                {
                    continue;
                }
                Thread.Sleep(new Random().Next(1, 5) * 1000);
                if (!WebHelper.CheckImgExits(SinaImg, UploadUtils.BlockImgInfo))
                {
                    Console.WriteLine("链接失效{0}，时间：{1}", SinleImgInfo, DateTime.Now.ToString());
                    WebmodeIns(OrgImg);
                    continue;
                }
                if (SingleLink(SinaImg, ProportionCount))
                {
                    Console.WriteLine("宽高不符合标准{0}，时间：{1}", SinleImgInfo, DateTime.Now.ToString());

                    continue;
                }

                CompltedList.Add(string.Format(SinaStaticvalue.ImgInfoText, SinaImg, OrgImg));
            }
            FileHelper.AppendList(FileNameHelper.CreateFileNameWithChildFloder(XmlFilePath, "检查失效和比例"), CompltedList);
            FileHelper.FileDelete(XmlFilePath);
        }

        private void WebmodeIns(string orgImg)
        {
            string FileName = FileNameHelper.GetFileNameWithoutExtension(orgImg);
            string API = "http://acg12.net/acgtemp/pixivexclude.php?filename={0}&pass=cat00121425&mode=ins";
            WebHelper.HttpPcGet(string.Format(API, FileName));
        }

        /// <summary>
        /// 云模式，发现需要跳出的图片返回true
        /// </summary>
        /// <param name="orgImg"></param>
        /// <returns></returns>
        private bool WebModeExt(string orgImg)
        {
            string FileName = FileNameHelper.GetFileNameWithoutExtension(orgImg);
            String API = "http://acg12.net/acgtemp/pixivexclude.php?filename={0}&pass=cat00121425&mode=ext";
            string Shtml = WebHelper.HttpPcGet(string.Format(API, FileName));
            if (HtmlHelper.GetTextValueList(Shtml, "//exit").Count > 0)
            {
                Console.WriteLine("云模式，已经发现违规图片。");
                return true;
            }
            return false;
        }

        /// <summary>
        /// 判断宽高比是否通过，需要跳出返回true
        /// </summary>
        /// <param name="ImgLink"></param>
        /// <param name="ProportionCount"></param>
        /// <returns></returns>
        public bool SingleLink(string ImgLink, float ProportionCount)
        {
            String FilePath = new WebHelper().DownloadToFloder(ImgLink, StaticValue.DownloadPath);

            FilePath = SingleFile(FilePath, ProportionCount);
            if (FileHelper.FileExits(FilePath))//如果文件还存在，则代表未触发移动（不符合规定才移动）
            {
                FileHelper.FileDelete(FilePath);
                return false;
            }
            return true;
        }

        /// <summary>
        /// 检查图片是否符合比例，不符合的则移动
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="ProportionCount"></param>
        /// <returns></returns>
        public string SingleFile(string FilePath, float ProportionCount)
        {
            try
            {
                System.Drawing.Image image = System.Drawing.Image.FromFile(FilePath);

                float PercentageCount = (float)image.Width / image.Height;
                image.Dispose();
                Console.WriteLine("比例为{0}", PercentageCount);
                if (PercentageCount < ProportionCount)
                {
                    Console.WriteLine("文件已经移动到此目录的子文件（Doubt）下");
                    string childrenFloder = FloderNameHelper.GetFloderPath(FilePath) + "Doubt\\";

                    if (FloderHelper.FloderExits(childrenFloder, true))
                    {
                        Console.WriteLine(childrenFloder + FileNameHelper.GetFileName(FilePath));
                        string MoveFilePath = childrenFloder + FileNameHelper.GetFileName(FilePath);
                        FileHelper.FileMove(FilePath, MoveFilePath);
                        // return MoveFilePath;
                    }
                }
            }
            catch (Exception ex) { PrintLog.Log(ex); }
            return FilePath;
        }
    }
}